import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.WKTReader;

public class LocationTest {
    /**
     *
     几何图形计算 2024年8月30日
     <dependency>
         <groupId>org.locationtech.jts</groupId>
         <artifactId>jts-core</artifactId>
         <version>1.20.0</version>
     </dependency>
     */
    public static void main(String[] args) throws Exception  {
        // 创建 GeometryFactory，用来生成点和多边形
        GeometryFactory geometryFactory = new GeometryFactory();

        // 多边形的 WKT 格式表示
        String polygonWKT = "POLYGON ((0 0, 5 0, 5 5, 0 5, 0 0))";
        WKTReader reader = new WKTReader(geometryFactory);

        // 解析多边形
        Polygon polygon = (Polygon) reader.read(polygonWKT);

        // 创建一个点（经纬度）
        Point point1 = geometryFactory.createPoint(new Coordinate(3, 3)); // 点在多边形内
        Point point2 = geometryFactory.createPoint(new Coordinate(5, 0)); // 点在边上
        Point point3 = geometryFactory.createPoint(new Coordinate(6, 6)); // 点在外面

        // 判断点是否在多边形内、外或者边上
        System.out.println("Point 1 (3,3) inside polygon: " + isPointInPolygon(polygon, point1)); // inside
        System.out.println("Point 2 (5,0) on the edge: " + isPointOnEdge(polygon, point2)); // on the edge
        System.out.println("Point 3 (6,6) outside polygon: " + isPointInPolygon(polygon, point3)); // outside
    }

    // 判断点是否在多边形内部
    public static boolean isPointInPolygon(Polygon polygon, Point point) {
        return polygon.contains(point); // 返回true如果点在多边形内部
    }

    // 判断点是否在多边形的边上
    public static boolean isPointOnEdge(Polygon polygon, Point point) {
        return polygon.getBoundary().intersects(point); // 返回true如果点在边上
    }
}
